''' rapidget v0.2 by contmp '''

##changelog for v0.2
##- minor code optimalization
##- testing the filesize if the file is complete (in case of connection reset, etc) first download with extension .incomplete
##- adjusted the percent counting mechanism
##- compiled for windows with py2exe
import os
import urllib
import time

def getPage(url):
    u = urllib.urlopen(url)
    data = u.read()
    u.close()
    return data

def getPagePOST(url, params):
    u = urllib.urlopen(url,params)
    data = u.read()
    u.close()
    return data

def rapidget(url):
    html = getPage(url)
    print
    print 'attempting to download:', url
    ## export link action url from the form
    dform_from = html.find('<form id="ff" action="')
    dfrom_to = html.find('</form>', dform_from)

    dform = html[dform_from : dfrom_to]
    dlist = dform.split('\n')[0]
    action = dlist.split(' ')[2]
    action = action.replace('action="', '')
    url = action.replace('"', '')
    ## end of comment

    ## choose free rs account
    params = urllib.urlencode({"dl.start": "Free", "submit": "Free user"})
    html = getPagePOST(url, params)
    ## end of comment

    ## extract time delay value (c value)
    od = html.find('var c=') + len('var c=')
    c = int( html[od:].split('\n')[0].replace(' ', '').replace(';', '').replace('\r', '') )
    c += 2 # safe
    ## end of comment

    ## extract download link
    dform_from = html.find('<form name="dlf" action="')
    dfrom_to = html.find('" method="post"', dform_from)

    dform = html[dform_from : dfrom_to]
    action = dform
    action = action.replace('<form name="dlf" action="', '')
    url = action.replace(' ', '')
    ## end of comment

    ## finally attempt to download the file
    params = urllib.urlencode({"name": "dlf"})
    html = getPagePOST(url, params)
    ## end of comment
    print 'waiting', c, 'seconds:',
    while c!=0:
        time.sleep(1)
        print c,
        c -= 1
    print
    filename = url.split('/')[6]
    u = urllib.urlopen(url)
    clen = int(u.info()["Content-length"])
    print 'downloading', filename, '(', clen/1024**2, ') MB'
    f = open(filename+'.incomplete', 'wb')

    data = ''
    n = 0
    perc = -1
    while 1:
        p = n/(clen/100)
        if p != perc:
            print str(p)+'%',
            perc = p
        buf = u.read(1024*1024/5)# buffer of size 200kb
        if not buf:
            break
        n += len(buf)
        f.write(buf)
    print
    f.close()
    u.close()
    if n == clen:
        os.rename(filename+'.incomplete', filename)
        print filename, 'saved!', "(", n, 'bytes )'
    else:
        print 'FATAL ERROR: data lost! (probably the connection was reset)'
    print

## load list of urls from the file 
f = open('rapidshareget.urls.txt')
data = f.read()
f.close()
## end of comment

for url in data.split('\n'):
    if url != '':
        try:
            rapidget(url)
        except:
            print 'CRITICAL ERROR! (probably the file is no longer available)'
